package www.cslc.com.Util;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XMLUtil {
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public   List<String> readSystem() throws Exception{
        Document doc = null;
        List list = null;
        List<String> retList= new ArrayList();
        SAXReader reader = new SAXReader();
        reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        File f = new File("conf/interfaceMessage.xml");
        doc = reader.read(f);
        doc.selectNodes("/data/system");
        list = doc.selectNodes("/data/system/systemType");
        if (list != null && list.size() > 0) {
            retList = new ArrayList<String>();
            for (Iterator it = list.iterator(); it.hasNext();) {
                Element e = (Element) it.next();
                retList.add(e.getTextTrim());
            }
        }
        return retList;
    }

  @SuppressWarnings({ "unchecked", "rawtypes" })
  public  List<String> readInterfaceType(final String system) throws Exception{
        Document doc = null;
        List list = null;
        List<String> retList= new ArrayList();
        SAXReader reader = new SAXReader();
        reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        File f = new File("conf/interfaceMessage.xml");
        doc = reader.read(f);
        List subs = doc.selectNodes("/data/interfaceType");
        for(Iterator ite = subs.iterator(); ite.hasNext();){
            Element el = (Element) ite.next();
            if(system.equals(el.attribute(0).getText())){
                list=el.selectNodes("interface");
                if (list != null && list.size() > 0) {
                    retList = new ArrayList<String>();
                    for (Iterator it = list.iterator(); it.hasNext();) {
                        Element e = (Element) it.next();
                        retList.add(e.getTextTrim());
                    }
                }
            }
        }
        return retList;
    }
}
